package com.qingxin.inspection.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qingxin.cms.entity.InspectionItem;
import com.qingxin.cms.entity.InspectionTask;
import com.qingxin.inspection.action.InspectionItemAttachmentService;
import com.qingxin.park.entity.FarmPark;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.entity.MyUploadFileParam;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
@Service
public class InspectionItemService extends BaseService<InspectionItem, String>{
	
	@Autowired
	private InspectionItemAttachmentService attachmentService;
	/**
	 * 获取巡查项目分页数据
	 * @param pageParam
	 * @param search
	 * @param taskId
	 * @param request
	 * @return
	 */
	public Pager list(PageParam pageParam,String search,String taskId, HttpServletRequest request){
		Querier querier = Querier.create("select new com.qingxin.cms.entity.InspectionItem(" +
				"itemId,itemName,createBy,createTime,updateBy,updateTime,finishBy,finishTime)" +
				"from InspectionItem where inspectionTask.taskId=:taskId");
		querier.setParam("taskId", taskId);
		if(StringUtils.isNotEmpty(search)){
			search = "%"+search.replace("'", "")+"%";
			querier.append(" and (itemName like :itemName)");
			querier.setParam("itemName", search);
		}
		querier.append(pageParam.getOrderby(" order by createTime desc"));
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	/**
	 * 添加巡查项目
	 * @param item
	 * @param result
	 * @param loginUser
	 * @param request
	 * @return
	 */
	
	public SysError add(InspectionItem item, String taskid,String parkid,MyUploadFileParam uploadFileParam,LoginUser loginUser, HttpServletRequest request){
		Date now = new Date();
		item.setCreateTime(now);
		item.setUpdateTime(now);
		item.setCreateBy(loginUser.getLogin_name());
		item.setUpdateBy(loginUser.getLogin_name());
		item.setInspectionTask(new InspectionTask(taskid));
		item.setFarmPark(new FarmPark(parkid));
		this.save(item);
		
		//添加附件 
		attachmentService.saveAttachment(item.getItemId(), uploadFileParam, loginUser);
		return null;
	}
	
	/**
	 * 修改巡查项目信息
	 * @param item
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(InspectionItem item, String parkid, MyUploadFileParam uploadFileParam,LoginUser loginUser, HttpServletRequest request){
		InspectionItem old = getSafeEntity(item.getItemId());
		String fields = "itemName,remark,finishBy,finishTime,result";
		BeanUtil.setTargetFields(old, item, fields, true);
		old.setUpdateBy(loginUser.getLogin_name());
		old.setUpdateTime(new Date());
		old.setFarmPark(new FarmPark(parkid));
		Updater<InspectionItem> updater = new Updater<>(old, fields+",updateBy,updateTime");
		this.update(updater);
		attachmentService.saveAttachment(item.getItemId(), uploadFileParam, loginUser);
		return null;
	}
	
	/**
	 * 删除巡查项目信息
	 * @param itemId
	 * @param request
	 * @return
	 */
	public SysError delete(String[] itemId, HttpServletRequest request){
		for(String id : itemId){
			InspectionItem item = this.get(id);
			if(item == null){
				continue;
			}
			this.delete(item);
		}
		return null;
	}
	
	
	/**
	 * 获取所有园区
	 * @param parkid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<FarmPark> getParkData(LoginUser loginUser){
		StringBuilder hql = new StringBuilder();
		hql.append("select new com.qingxin.park.entity.FarmPark(p.parkid, p.parkName)");
		hql.append(" from FarmPark p where p.isdel='0'");
		if(!SystemCfg.isDataAdminUser(loginUser)){
			hql.append(" and p.parkid in (select distinct v.farmPark.parkid from SysUserParkRel v where v.sysUser.userid='"+loginUser.getUserid()+"')");
		}
		hql.append(" order by p.parkName asc");
		List<FarmPark> list = listByHql(hql.toString());
		
		/*List<FarmPark> list = listByHql("select new com.qingxin.park.entity.FarmPark(parkid,parkName)" +
				" from FarmPark" +
				" order by createTime asc");*/
		if(list == null){
			return new ArrayList<FarmPark>();
		}
		return list;
	}
	
	/**
	 * 判断指定项目名称是否被使用
	 * @param itemName
	 * @param itemId
	 * @param taskId
	 * @return
	 */
	public boolean isNameDuplicate(String itemName, String itemId, String taskId){
		if(StringUtils.isEmpty(itemId)){
			Object obj = getByHql("select itemId from InspectionItem where inspectionTask.taskId=? and itemName=?", taskId, itemName);
			return obj!=null;
		}else {
			Object obj = getByHql("select itemId from InspectionItem where inspectionTask.taskId=? and itemName=? and itemId<>?",taskId ,itemName, itemId);
			return obj!=null;
		}
	}

}
